import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basecomponent/BaseComponent.dart';
import 'package:flutter_ahj_app/base/basewidget/viewmodel/BaseViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/viewmodel/ViewModelProvider.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/widget/LoadingDialog.dart';

abstract class BaseStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => initState();

  State initState();
}

abstract class BaseStatelessWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) => initState();

  Widget initState();
}

abstract class BaseState<T extends BaseViewModel, S extends StatefulWidget>
    extends BaseComponent<S> {
  T viewModel;

  @override
  void initState() {
    super.initState();
    viewModel = ViewModelProvider.getViewModel(context);
    viewModel.init(context);

    viewModel.errorStream().listen((result) {
      if (result != null &&
          result.response != null &&
          result.response.statusCode != null) {
        isTokenFailure(
            result.response.statusCode.toString(), result.response.data);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return initWidget();
  }

  /*
  * 判断登录是不是失效
  * */
  bool isTokenFailure(String code, var data) {
    bool flag = true;
    if ("426" == code) {
      ToastUtils.showToast("账号密码错误");
    } else if ("401" == code) {
      //账户失效
      UserInfoUtils.setUserToken("");
      Navigator.pushNamedAndRemoveUntil(
          context, "/LoginPageView", (check) => false);
    } else if ("400" == code) {
      if (data['code'] == '401') {
        //账户失效
        UserInfoUtils.setUserToken("");
        Navigator.pushNamedAndRemoveUntil(
            context, "/LoginPageView", (check) => false);
      } else {
        ToastUtils.showToast(data['msg']);
      }
    } else {
      ToastUtils.showToast("系统异常");
      flag = false;
    }
    return flag;
  }

  BuildContext dialogContext;
  void showCustomDialog(BuildContext context, {String text}) {
    showDialog(
        context: context,
        builder: (BuildContext mcontext) {
          dialogContext = mcontext;
          return LoadingDialog(text: text ?? "加载中");
        });
  }

  void closeCustomDialog() {
    if (dialogContext != null) {
      Navigator.pop(dialogContext);
      dialogContext = null;
    }
  }

  /*
  * 判断字段是不是null
  * */
  bool isEmpty(Object value) {
    if (value == null || value == "") return true;
    if (value is String && value.isEmpty) {
      return true;
    }
    return false;
  }

  String getString(String text,
      {Key key, String befor = "", String after = "", String defaultStr = ""}) {
    return befor + (text == null ? defaultStr : text) + after;
  }

  Widget initWidget();

  @override
  void dispose() {
    viewModel.dispose();
    super.dispose();
  }
}
